package com.qing.springboot01.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qing.springboot01.common.R;
import com.qing.springboot01.dto.DishDto;
import com.qing.springboot01.dto.OrdersDto;
import com.qing.springboot01.dto.OrdersVo;
import com.qing.springboot01.entity.Category;
import com.qing.springboot01.entity.Dish;
import com.qing.springboot01.entity.OrderDetail;
import com.qing.springboot01.entity.Orders;
//import com.qing.springboot01.entity.OrdersVo;
import com.qing.springboot01.mapper.OrderDetailMapper;
import com.qing.springboot01.mapper.OrdersMapper;
import com.qing.springboot01.service.OrderDetailService;
import com.qing.springboot01.service.OrdersService;
//import com.qing.springboot01.service.OrdersVoService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author tina
 * @since 2023-06-26 04:09:20
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrdersController {

    /*
    * 订单分页查询
     **/
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private OrderDetailMapper orderDetailMapper;
//    @Autowired
//    private OrdersVoService ordersVoService;
    @Autowired
    OrdersMapper ordersMapper;

    @GetMapping("/userPage")
    public R<Page> orderPaging(int page,int pageSize){
         Page<Orders> ordersInfo = new Page<>(page, pageSize);
         Page<OrdersDto> ordersDtoPage = new Page<>(page,pageSize);
         OrdersDto ordersDto = new OrdersDto();
//        // 先进行订单表的分页查询 值存入orderInfo
       ordersService.page(ordersInfo);
//        // 对象拷贝
        BeanUtils.copyProperties(ordersInfo,ordersDtoPage,"records");
//
        List<Orders> records = ordersInfo.getRecords();
        List<OrderDetail> orderDetails =new ArrayList<>();
        // 查询所有详情
        List<OrderDetail> list = orderDetailService.list();
        for (int i = 0; i < records.size(); i++) {
            orderDetails.clear();
            System.out.println("records.get(i).getId() = " + records.get(i).getId());
            System.out.println("list = " + list.size());
            for (OrderDetail d : list) {
                if(records.get(i).getId()==d.getOrderId()){
                    orderDetails.add(d);
                }
            }

            records.get(i).setOrderDetails(orderDetails);
        }

        System.out.println("records = " + records);

        return R.success(ordersInfo);
    }


    @GetMapping("/list")
    public R orderList(int page,int pageSize){
        Page page1=new Page(page,pageSize);
        Page page2 = ordersService.page(page1);
//        System.out.println(o.getOrdersVo());
        return R.success(page2);
    }

    // 再来一单

    @PostMapping("/again")
    public R orderAgain(@RequestBody Map<String,Object> map){
        LambdaQueryWrapper<Category> queryWrapper =new LambdaQueryWrapper<>();
        Orders orders = ordersMapper.selectById((Serializable) map.get("id"));
        System.out.println(orders);
        if(orders!=null){
            return R.success(orders);
        }else{
            return R.error("订单不存在");
        }
    }

    // 提交订单
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        ordersService.submit(orders);
        return R.success("");
    }


}
